<?php
/**
 * User: Allen
 * Date: 15-12-03
 * 管理员中心控制器
 */

class Node extends PC_Controller{
	
	protected $go_url = '/admin/node/node_list';	
	public function __construct(){
		parent::__construct();
		$this->load->model('node_model');	
	}
	
	/**
	 * @brief 节点列表
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function node_list() {
	 	$this->data['count'] = $this->node_model->getCount();
		if($this->data['count']) {
			$nodes = $this->node_model->getNodeAll();
			$nodes = $this->merge_node($nodes);
			$this->data['data'] = $nodes;
		}else {
			$this->data['data'] = array(); 
		}
		$message = 1;
		if($this->uri->segment(4)) {
			$message = $this->uri->segment(4);
		}
		$this->data['message'] = $message;
		//判断权限
		$this->data['authority'] = $this->authority;
		$this->load->view('/admin/node_list.php',$this->data);
	}
	
	
	/**
	 * @brief 加载节点编辑页面
	 * @param Null
	 * @par 2015/12/04 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['node_id'] = $id;
		$node_info = $this->node_model->checknode($where);
		$this->data['data'] = $node_info;
		$this->load->view('/admin/node_edit',$this->data);
	}

	/**
	 * @brief 提交节点编辑操作
	 * @param Null
	 * @par 2015/12/04 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$where['node_id'] = $data['node_id'];
		$add_result = $this->node_model->editNode($where,$data);
		$go_url = 'node_list';
		if($add_result) {
			$this->location_href($this->go_url."/2");
		}else{
			$this->location_href($this->go_url."/3");
		}
	}
	
	/**
	 * @brief 加载添加节点页面
	 * @param Null
	 * @par 2015/12/04 Ver 1.00 Created by Allen
	 */
	public function add_page() {
		$id = $this->uri->segment(4);
		$id = $id ? $id:0;
		$this->data['pid'] = $id;
		$this->load->view('/admin/node_add',$this->data);
	}
	
	/**
	 * @brief 提交节点添加操作
	 * @param Null
	 * @par 2015/12/04 Ver 1.00 Created by Allen
	 */
	public function add() {
		$data = $_POST;
		$add_result = $this->node_model->addnode($data);
		$go_url = 'node_list';
		if($add_result){
			$this->location_href($this->go_url."/4");
		}else{
			$this->location_href($this->go_url."/5");
		}
	}
			
	/**
	 * @brief 删除节点操作
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Update by Allen
	 * @par update 2015年10月13日  ver 2.00 create by chenqi  修改删除逻辑
	 */
	public function delete() {
		$data = $_POST;
		$id = $data['node_id'];	
		$node_info = $this->node_model->checkNode(array("node_id"=>$id));//获取节点信息
		$this->load->model('access_model');//加载access模型
		if($node_info['pid'] == 0) {
			//父节点
			$child_nodes = $this->node_model->getNodeAll(array("pid"=>$node_info['node_id']));//获取子节点
			foreach ($child_nodes as $k=>$node){
				//删除子节点
				if($this->access_model->deleteAccess(array("node_id"=>$node['node_id'])) ){//删除该节点对应角色权限
					$this->node_model->deleteNode(array("node_id"=>$node['node_id']));
				}
			}
			//删除父节点
			if($this->access_model->deleteAccess(array("node_id"=>$node_info['node_id'])) ) {//删除角色权限
				if( $this->node_model->deleteNode(array("node_id"=>$node_info['node_id'])) ) {//删除父节点
					echo json_encode(true);//删除成功
				}else {
					echo json_encode(false);//删除失败
				}
			}			
		}else {			
			//子节点
			if($this->access_model->deleteAccess(array("node_id"=>$id)) ){//删除角色权限
				if($this->node_model->deleteNode(array("node_id"=>$id)) ){//删除节点
					echo json_encode(true);//删除成功
				}else{
					echo json_encode(false);//删除失败
				}
			}
		}		

	}
}